const {ccclass, property} = cc._decorator;

import Http from '../common/util/Http'
var manager = require("XmdyManager")
@ccclass
export default class Hall extends cc.Component {

    @property(cc.Sprite)
    headImage:cc.Sprite= null;

    @property(cc.Label)
    nickname:cc.Label= null;

    @property(cc.EditBox)
    roomId:cc.EditBox = null;

    _http = null;
    start(){
        
    }

    createRoom(){
        this._http.getJson(Http.HALL_URL_PREFIX + "room/create",{token:manager.token})
        .subscribe((res)=>{
            cc.director.loadScene('Game')
            manager.room.roomId = res.roomId
        })
    }

    joinRoom(event,param){
        this._http.getJson(Http.HALL_URL_PREFIX + "room/join",{token:manager.token,roomId:this.roomId.string})
        .subscribe((res)=>{
            
            manager.room.roomId = res.roomId
            cc.director.loadScene('Game')
            
        })
       
        
    }
    @property({type:cc.Label})
    notify:cc.Label = null;

    socket = null;
    connectSocket(){
        this.socket = io('http://192.168.21.24:20011/hall');
        this.socket.on("notice",(res)=>{
            
            if(typeof res == 'string'){
                res = JSON.parse(res)
            }
            this.notify.string = res.data; 
        })
    }

    
    logout(){
        this.userPlugin.logout();
        cc.director.loadScene('Login') 
       
           
    }
 
    userPlugin = {logout:function(){cc.log("登出失败")}};
    onLoad() {
        if(cc.sys.isMobile){
            var agent = anysdk.agentManager;
            this.userPlugin = agent.getUserPlugin();
           
        }
        this.connectSocket();

        this._http = new Http();

        this.nickname.string = manager.user.nickname;
        /*
        let texture = cc.textureCache.getTextureForKey(manager.user.headImage);
        if(texture == null){
            cc.loader.load({url:manager.user.headImage},   (err, tx) =>{
                
                console.log("load headImage error",err)
                
                this.headImage.spriteFrame = new cc.SpriteFrame(tx);
                cc.loader.setAutoRelease(tx, true);
            });
        }else{
            console.log("texture is not null")
            this.headImage.spriteFrame = new cc.SpriteFrame(texture);
        }
        */
    }

    onDestroy(){
       
        if(this.socket){
            this.socket.disconnect();
        }
    }
}
